package com.mycompany.app.common.scheduler.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.mycompany.app.common.entity.ToStringEntity;

@SuppressWarnings("serial")
@Entity
@Table(name = "daemon_job_trigger")
public class DaemonJobTrigger extends ToStringEntity {
    protected static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(DaemonJobTrigger.class);

    public static enum IntervalType {
        UNDEFINED, ONLY_ONCE, DAILY, WEEKLY, MONTHLY, YEARLY, RIGHT_NOW
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String description;

    @Enumerated(EnumType.STRING)
    @Column(name = "interval_type", nullable = false)
    private IntervalType intervalType;

    @Column(name = "RUN_YEAR")
    private Integer year = 0;

    @Column(name = "RUN_MONTH")
    private Integer month = 0;

    @Column(name = "RUN_DAY")
    private Integer dayOfMonth = 0;

    @Column(name = "RUN_WEEKDAY")
    private Integer dayOfWeek = 0;

    @Transient
    private Integer hour = 0;

    @Transient
    private Integer minute = 0;

//    private String time;

    @ManyToOne(cascade=CascadeType.REFRESH)
    @JoinColumn(name = "daemon_job_id")
    private DaemonJob daemonJob;

    @Column(name = "RUN_TIME", nullable = true)
    public String getTime() {

        return this.getHour() + ":" + this.getMinute();
    }

    public void setTime(String time) {

        int index = (time = time.trim()).indexOf(":");

        String strHour, strMnt;
        if (index == -1) {
            strHour = time;
            strMnt = "0";
        } else {
            strHour = time.substring(0, index);
            strMnt = time.substring(index + 1, time.length());
        }
        try {
            this.hour = Integer.parseInt(strHour);
        } catch (Exception e) {
            log.error(e);
            this.hour = 0;
        }
        try {
            this.minute = Integer.parseInt(strMnt);
        } catch (Exception e) {
            log.error(e);
            this.minute = 0;
        }
    }

    // ========================================================

    @Override
    public String toString() {

        return ToStringBuilder.reflectionToString(this,
                ToStringStyle.SHORT_PREFIX_STYLE);
    }

    // ========================================================

    public Long getId() {

        return id;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public String getDescription() {

        return description;
    }

    public void setDescription(String description) {

        this.description = description;
    }

    public IntervalType getIntervalType() {

        return intervalType;
    }

    public void setIntervalType(IntervalType intervalType) {

        this.intervalType = intervalType;
    }

    public DaemonJob getDaemonJob() {

        return daemonJob;
    }

    public void setDaemonJob(DaemonJob job) {

        this.daemonJob = job;
    }

    public Integer getYear() {

        return year;
    }

    public void setYear(Integer year) {

        this.year = year;
    }

    public Integer getMonth() {

        return month;
    }

    public void setMonth(Integer month) {

        this.month = month;
    }

    public Integer getDayOfMonth() {

        return dayOfMonth;
    }

    public void setDayOfMonth(Integer dayOfMonth) {

        this.dayOfMonth = dayOfMonth;
    }

    public Integer getDayOfWeek() {

        return dayOfWeek;
    }

    public void setDayOfWeek(Integer dayOfWeek) {

        this.dayOfWeek = dayOfWeek;
    }

    public Integer getHour() {

        return hour;
    }

    public void setHour(Integer hour) {

        this.hour = hour;
    }

    public Integer getMinute() {

        return minute;
    }

    public void setMinute(Integer minute) {

        this.minute = minute;
    }

}
